package com.ruoyi.game.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.game.mapper.GamePlayerMapper;
import com.ruoyi.game.domain.GamePlayer;
import com.ruoyi.game.service.IGamePlayerService;

/**
 * 玩家信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-18
 */
@Slf4j
@Service
public class GamePlayerServiceImpl implements IGamePlayerService 
{
    @Autowired
    private GamePlayerMapper gamePlayerMapper;

    /**
     * 查询玩家信息
     * 
     * @param id 玩家信息主键
     * @return 玩家信息
     */
    @Override
    public GamePlayer selectGamePlayerById(Long id)
    {
        return gamePlayerMapper.selectGamePlayerById(id);
    }

    /**
     * 查询玩家信息列表
     * 
     * @param gamePlayer 玩家信息
     * @return 玩家信息
     */
    @Override
    public List<GamePlayer> selectGamePlayerList(GamePlayer gamePlayer)
    {
        return gamePlayerMapper.selectGamePlayerList(gamePlayer);
    }

    /**
     * 新增玩家信息
     * 
     * @param gamePlayer 玩家信息
     * @return 结果
     */
    @Override
    public int insertGamePlayer(GamePlayer gamePlayer)
    {
        gamePlayer.setCreateTime(DateUtils.getNowDate());
        return gamePlayerMapper.insertGamePlayer(gamePlayer);
    }

    /**
     * 修改玩家信息
     * 
     * @param gamePlayer 玩家信息
     * @return 结果
     */
    @Override
    public int updateGamePlayer(GamePlayer gamePlayer)
    {
        gamePlayer.setUpdateTime(DateUtils.getNowDate());
        return gamePlayerMapper.updateGamePlayer(gamePlayer);
    }

    /**
     * 批量删除玩家信息
     * 
     * @param ids 需要删除的玩家信息主键
     * @return 结果
     */
    @Override
    public int deleteGamePlayerByIds(Long[] ids)
    {
        return gamePlayerMapper.deleteGamePlayerByIds(ids);
    }

    /**
     * 删除玩家信息信息
     * 
     * @param id 玩家信息主键
     * @return 结果
     */
    @Override
    public int deleteGamePlayerById(Long id)
    {
        return gamePlayerMapper.deleteGamePlayerById(id);
    }

    @Override
    public boolean existsByUsername(String username) {
        return gamePlayerMapper.countByUsername(username) > 0;
    }

    @Override
    public GamePlayer selectByUsername(String username) {
        return gamePlayerMapper.selectByUsername(username);
    }
    @Override
    public Long getRoomcardByPlayerId(Long playerId) {
        GamePlayer player = gamePlayerMapper.selectGamePlayerById(playerId);
        if (player == null) {
            throw new IllegalArgumentException("玩家不存在");
        }
        return player.getRoomCard();
    }

    @Override
    public Long recharge(Long playerId, Long amount, String remark) {
        GamePlayer player = gamePlayerMapper.selectGamePlayerById(playerId);
        if (player == null) {
            throw new IllegalArgumentException("玩家不存在");
        }

        int updated = gamePlayerMapper.updateRoomCardById(playerId, amount);
        if (updated > 0) {
            // ✅ 你也可以在这里记录充值流水（game_room_card_log）
            log.info("[充值] 玩家 {} 增加房卡 {}，备注：{}", playerId, amount, remark);
            return player.getRoomCard() + amount;
        } else {
            throw new RuntimeException("充值失败");
        }
    }

}
